import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui'
Vue.use(VueRouter)

const routes = [
  { path: '/', name: 'begin', redirect: '/index' },
  {
    path: '/index',
    name: 'index',
    redirect: '/index/discover',
    component: () => import('@/views/home/index.vue'),
    // 二级路由
    children: [
      {
        path: '/index/discover',
        name: 'discover',
        redirect: '/index/discover/recommend',
        component: () => import('@/views/Discover/index.vue'),
        // 三级路由
        children: [
          {
            path: '/index/discover/recommend',
            name: 'recommend',
            component: () =>
              import('@/views/Discover/disCoverChildren/recommend.vue')
          },
          {
            path: '/index/discover/musicList',
            name: 'musicList',
            redirect: '/index/Discover/musicList/musicListIndex',
            component: () =>
              import('@/views/Discover/disCoverChildren/musicList.vue'),
            children: [
              {
                path: '/index/Discover/musicList/musicListIndex',
                name: 'musicListIndex',
                component: () =>
                  import(
                    '@/views/Discover/disCoverChildren/musicListChildren/musicListIndex.vue'
                  )
              }
            ]
          },
          {
            path: '/index/discover/ranking',
            name: 'ranking',
            component: () =>
              import('@/views/Discover/disCoverChildren/ranking.vue')
          },
          {
            path: '/index/discover/singer',
            name: 'singer',
            component: () =>
              import('@/views/Discover/disCoverChildren/singer.vue')
          }
        ]
      },
      {
        path: '/index/video',
        name: 'video',
        component: () => import('@/views/video/index.vue'),
        redirect: '/index/video/videoPage',
        children: [
          {
            path: '/index/video/Mv',
            name: 'Mv',
            component: () => import('@/views/video/components/MVPage.vue')
          },
          {
            path: '/index/video/videoPage',
            name: 'videoPage',
            component: () => import('@/views/video/components/videoPage.vue')
          }
        ]
      },
      {
        path: '/index/collect',
        component: () => import('@/views/collect/index.vue'),
        redirect: '/index/collect/album',
        name: 'collect',
        children: [
          {
            path: '/index/collect/album',
            name: 'collectalbum',
            component: () =>
              import('@/views/collect/collectChildern/albumPage.vue')
          },
          {
            path: '/index/collect/song',
            name: 'collectsong',
            component: () =>
              import('@/views/collect/collectChildern/songPage.vue')
          },
          {
            path: '/index/collect/video',
            name: 'collectvideo',
            component: () =>
              import('@/views/collect/collectChildern/videoCollectPage.vue')
          }
        ]
      },
      {
        path: '/index/nobreak',
        name: 'nobreak',
        component: () => import('@/views/nobreak/index.vue')
      },
      {
        path: '/index/musicDateil',
        name: 'musicDateil',
        component: () => import('@/views/musicDetail/index.vue')
      },
      // 查看用户信息
      {
        path: '/index/personal',
        name: 'personal',
        component: () => import('@/views/personal/index.vue')
      },
      {
        path: '/index/singerDetail',
        name: 'singerDetail',
        component: () => import('@/views/singerDetail/index.vue')
      },
      {
        path: '/index/album',
        name: 'album',
        component: () => import('@/views/album/index.vue')
      },
      {
        path: '/index/VideoDetail',
        name: 'VideoDetail',
        component: () => import('@/views/Mv/index.vue')
      },
      // 搜索页
      {
        path: '/index/search',
        name: 'search',
        component: () => import('@/views/search/index.vue'),
        redirect: '/index/search/song',
        children: [
          {
            path: '/index/search/song',
            name: 'searchSong',
            component: () => import('@/views/search/components/searchSong.vue')
          },
          {
            path: '/index/search/singer',
            name: 'searchSinger',
            component: () =>
              import('@/views/search/components/searchSinger.vue')
          },
          {
            path: '/index/search/album',
            name: 'searchAlbum',
            component: () => import('@/views/search/components/searchAlbum.vue')
          },
          {
            path: '/index/search/video',
            name: 'searchVideo',
            component: () => import('@/views/search/components/searchVideo.vue')
          },
          {
            path: '/index/search/musicList',
            name: 'searchMusicList',
            component: () =>
              import('@/views/search/components/searchMusicList.vue')
          }
        ]
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 路由守卫，未登录禁止访问
router.beforeEach((to, from, next) => {
  console.log(to.name)
  if (
    to.name === 'musicDateil' ||
    to.name === 'recommend' ||
    to.name === 'musicListIndex' ||
    to.name === 'ranking' ||
    to.name === 'singer' ||
    to.name === 'personal'
  ) {
    next()
  } else {
    if (!localStorage.getItem('userId')) {
      Message.warning('很抱歉,这次不行!请登录后重试!')
      // console.log('很抱歉,这次不行!请登录后重试!')
      // location.href = '/'
      // location.reload()
      return '/index'
    } else {
      next()
    }
  }
})
export default router
